<template>
    <div class="passchange">
        <div>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                     class="demo-ruleForm">
                <el-form-item label="旧密码：" prop="oldpass" style="width: 0px;">
                    <el-input placeholder="请输入旧密码" type="password" v-model="ruleForm.comppass"
                              autocomplete="off" style="width: 300px">
                    </el-input>
                </el-form-item>
                <el-form-item label="新密码：" prop="newpass">
                    <el-input placeholder="请输入新密码" type="password" v-model="ruleForm.newpass"
                              autocomplete="off" style="width: 300px">
                    </el-input>
                </el-form-item>
                <el-form-item label="新密码确认：" prop="newpass">
                    <el-input placeholder="再次请输入新密码" type="password" v-model="ruleForm.checkPass"
                              autocomplete="off" style="width: 300px">
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" style="width: 150px" @click="submitForm('ruleForm')">保存</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        name: 'passchange',
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.comppass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    comppass: '',
                    newpass: '',
                    checkPass: '',
                },
                rules: {
                    comppass: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    newpass: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    checkPass: [
                        {validator: validatePass2, trigger: 'blur'}
                    ],
                }
            }
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        alert('submit!');
                    } else {
                        return false;
                    }
                });
            },
        }
    }
</script>

<style scoped>
    .passchange {
        display: flex;
        flex-direction: row;
        justify-content: center;
        margin-top: 5%;

    }
</style>
